import re

from common.BasePages import BasePage


class TouchScreenPage(BasePage):
    # 触摸屏管理
    def touch_screen_url(self):
        self.goto_url("yncManagement")

    def create_touch_screen(self):
        self.page.get_by_role("button", name="新增").click()
        self.page.get_by_role("dialog", name="触摸屏新增").get_by_placeholder("请输入触摸屏名称").click()
        self.page.get_by_role("dialog", name="触摸屏新增").get_by_placeholder("请输入触摸屏名称").fill("张院龙触摸屏")
        self.page.get_by_role("textbox", name="请选择网关").click()
        self.page.get_by_role("listitem").filter(has_text="测试网关").click()
        self.page.locator("div").filter(has_text=re.compile(r"^触摸屏Ip\.\.\.触摸屏端口$")).get_by_role("textbox").nth(
            3).click()
        self.page.locator("div").filter(has_text=re.compile(r"^触摸屏Ip\.\.\.触摸屏端口$")).get_by_role(
            "textbox").first.click()
        self.page.locator("div").filter(has_text=re.compile(r"^触摸屏Ip\.\.\.触摸屏端口$")).get_by_role(
            "textbox").first.fill("192")
        self.page.get_by_label("触摸屏新增").locator("form div").filter(
            has_text="触摸屏Ip... 请输入IP地址 触摸屏端口").get_by_role("textbox").nth(1).fill("168")
        self.page.get_by_label("触摸屏新增").locator("form div").filter(
            has_text="触摸屏Ip... 请输入IP地址 触摸屏端口").get_by_role("textbox").nth(2).fill("110")
        self.page.get_by_label("触摸屏新增").locator("form div").filter(
            has_text="触摸屏Ip... 请输入IP地址 触摸屏端口").get_by_role("textbox").nth(3).fill("210")
        self.page.get_by_placeholder("请输入端口号").click()
        self.page.get_by_placeholder("请输入端口号").fill("502")
        self.page.get_by_role("textbox", name="请输入用户名").click()
        self.page.get_by_role("textbox", name="请输入用户名").fill("zhangyuanlong")
        self.page.get_by_role("textbox", name="请输入密码").click()
        self.page.get_by_role("textbox", name="请输入密码").fill("123456")
        self.page.get_by_role("button", name="确 定").click()
        self.assert_alert_content("保存成功")

    def edit_touch_screen(self):
        self.page.get_by_role("cell", name="  ").locator("i").nth(1).click()
        self.page.get_by_role("textbox", name="请输入密码").click()
        self.page.get_by_role("textbox", name="请输入密码").press("ControlOrMeta+a")
        self.page.get_by_role("textbox", name="请输入密码").fill("123456")
        self.page.get_by_role("button", name="确 定").click()
        assert self.page.get_by_text("保存成功")

    def delete_touch_screen(self):
        self.page.locator(
            ".el-table__fixed-header-wrapper > .el-table__header > thead > tr > .el-table_1_column_1 > .cell > .el-checkbox > .el-checkbox__input > .el-checkbox__inner").first.click()
        self.page.get_by_role("button", name="批量删除").click()
        self.page.get_by_role("button", name="确定").click()
        assert self.page.get_by_text("删除成功")
